home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zmatrix.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.2 KB  |  364 lines

  1. /* Copyright (C) 1989, 1995, 1997, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zmatrix.c,v 1.4 2000/09/19 19:00:54 lpd Exp $ */
  20. /* Matrix operators */
  21. #include "ghost.h"
  22. #include "oper.h"
  23. #include "igstate.h"
  24. #include "gsmatrix.h"
  25. #include "gscoord.h"
  26. #include "store.h"
  27.  
  28. /* Forward references */
  29. private int common_transform(P3(i_ctx_t *,
  30.         int (*)(P4(gs_state *, floatp, floatp, gs_point *)),
  31.         int (*)(P4(floatp, floatp, const gs_matrix *, gs_point *))));
  32.  
  33. /* - initmatrix - */
  34. private int
  35. zinitmatrix(i_ctx_t *i_ctx_p)
  36. {
  37.     return gs_initmatrix(igs);
  38. }
  39.  
  40. /* <matrix> defaultmatrix <matrix> */
  41. private int
  42. zdefaultmatrix(i_ctx_t *i_ctx_p)
  43. {
  44.     os_ptr op = osp;
  45.     gs_matrix mat;
  46.  
  47.     gs_defaultmatrix(igs, &mat);
  48.     return write_matrix(op, &mat);
  49. }
  50.  
  51. /* - .currentmatrix <xx> <xy> <yx> <yy> <tx> <ty> */
  52. private int
  53. zcurrentmatrix(i_ctx_t *i_ctx_p)
  54. {
  55.     os_ptr op = osp;
  56.     gs_matrix mat;
  57.     int code = gs_currentmatrix(igs, &mat);
  58.  
  59.     if (code < 0)
  60.     return code;
  61.     push(6);
  62.     code = make_floats(op - 5, &mat.xx, 6);
  63.     if (code < 0)
  64.     pop(6);
  65.     return code;
  66. }
  67.  
  68. /* <xx> <xy> <yx> <yy> <tx> <ty> .setmatrix - */
  69. private int
  70. zsetmatrix(i_ctx_t *i_ctx_p)
  71. {
  72.     os_ptr op = osp;
  73.     gs_matrix mat;
  74.     int code = float_params(op, 6, &mat.xx);
  75.  
  76.     if (code < 0)
  77.     return code;
  78.     if ((code = gs_setmatrix(igs, &mat)) < 0)
  79.     return code;
  80.     pop(6);
  81.     return 0;
  82. }
  83.  
  84. /* <matrix|null> .setdefaultmatrix - */
  85. private int
  86. zsetdefaultmatrix(i_ctx_t *i_ctx_p)
  87. {
  88.     os_ptr op = osp;
  89.     int code;
  90.  
  91.     if (r_has_type(op, t_null))
  92.     code = gs_setdefaultmatrix(igs, NULL);
  93.     else {
  94.     gs_matrix mat;
  95.  
  96.     code = read_matrix(op, &mat);
  97.     if (code < 0)
  98.         return code;
  99.     code = gs_setdefaultmatrix(igs, &mat);
  100.     }
  101.     if (code < 0)
  102.     return code;
  103.     pop(1);
  104.     return 0;
  105. }
  106.  
  107. /* <tx> <ty> translate - */
  108. /* <tx> <ty> <matrix> translate <matrix> */
  109. private int
  110. ztranslate(i_ctx_t *i_ctx_p)
  111. {
  112.     os_ptr op = osp;
  113.     int code;
  114.     double trans[2];
  115.  
  116.     if ((code = num_params(op, 2, trans)) >= 0) {
  117.     code = gs_translate(igs, trans[0], trans[1]);
  118.     if (code < 0)
  119.         return code;
  120.     } else {            /* matrix operand */
  121.     gs_matrix mat;
  122.  
  123.     /* The num_params failure might be a stack underflow. */
  124.     check_op(2);
  125.     if ((code = num_params(op - 1, 2, trans)) < 0 ||
  126.         (code = gs_make_translation(trans[0], trans[1], &mat)) < 0 ||
  127.         (code = write_matrix(op, &mat)) < 0
  128.         ) {            /* Might be a stack underflow. */
  129.         check_op(3);
  130.         return code;
  131.     }
  132.     op[-2] = *op;
  133.     }
  134.     pop(2);
  135.     return code;
  136. }
  137.  
  138. /* <sx> <sy> scale - */
  139. /* <sx> <sy> <matrix> scale <matrix> */
  140. private int
  141. zscale(i_ctx_t *i_ctx_p)
  142. {
  143.     os_ptr op = osp;
  144.     int code;
  145.     double scale[2];
  146.  
  147.     if ((code = num_params(op, 2, scale)) >= 0) {
  148.     code = gs_scale(igs, scale[0], scale[1]);
  149.     if (code < 0)
  150.         return code;
  151.     } else {            /* matrix operand */
  152.     gs_matrix mat;
  153.  
  154.     /* The num_params failure might be a stack underflow. */
  155.     check_op(2);
  156.     if ((code = num_params(op - 1, 2, scale)) < 0 ||
  157.         (code = gs_make_scaling(scale[0], scale[1], &mat)) < 0 ||
  158.         (code = write_matrix(op, &mat)) < 0
  159.         ) {            /* Might be a stack underflow. */
  160.         check_op(3);
  161.         return code;
  162.     }
  163.     op[-2] = *op;
  164.     }
  165.     pop(2);
  166.     return code;
  167. }
  168.  
  169. /* <angle> rotate - */
  170. /* <angle> <matrix> rotate <matrix> */
  171. private int
  172. zrotate(i_ctx_t *i_ctx_p)
  173. {
  174.     os_ptr op = osp;
  175.     int code;
  176.     double ang;
  177.  
  178.     if ((code = real_param(op, &ang)) >= 0) {
  179.     code = gs_rotate(igs, ang);
  180.     if (code < 0)
  181.         return code;
  182.     } else {            /* matrix operand */
  183.     gs_matrix mat;
  184.  
  185.     /* The num_params failure might be a stack underflow. */
  186.     check_op(1);
  187.     if ((code = num_params(op - 1, 1, &ang)) < 0 ||
  188.         (code = gs_make_rotation(ang, &mat)) < 0 ||
  189.         (code = write_matrix(op, &mat)) < 0
  190.         ) {            /* Might be a stack underflow. */
  191.         check_op(2);
  192.         return code;
  193.     }
  194.     op[-1] = *op;
  195.     }
  196.     pop(1);
  197.     return code;
  198. }
  199.  
  200. /* <matrix> concat - */
  201. private int
  202. zconcat(i_ctx_t *i_ctx_p)
  203. {
  204.     os_ptr op = osp;
  205.     gs_matrix mat;
  206.     int code = read_matrix(op, &mat);
  207.  
  208.     if (code < 0)
  209.     return code;
  210.     code = gs_concat(igs, &mat);
  211.     if (code < 0)
  212.     return code;
  213.     pop(1);
  214.     return 0;
  215. }
  216.  
  217. /* <matrix1> <matrix2> <matrix> concatmatrix <matrix> */
  218. private int
  219. zconcatmatrix(i_ctx_t *i_ctx_p)
  220. {
  221.     os_ptr op = osp;
  222.     gs_matrix m1, m2, mp;
  223.     int code;
  224.  
  225.     if ((code = read_matrix(op - 2, &m1)) < 0 ||
  226.     (code = read_matrix(op - 1, &m2)) < 0 ||
  227.     (code = gs_matrix_multiply(&m1, &m2, &mp)) < 0 ||
  228.     (code = write_matrix(op, &mp)) < 0
  229.     )
  230.     return code;
  231.     op[-2] = *op;
  232.     pop(2);
  233.     return code;
  234. }
  235.  
  236. /* <x> <y> transform <xt> <yt> */
  237. /* <x> <y> <matrix> transform <xt> <yt> */
  238. private int
  239. ztransform(i_ctx_t *i_ctx_p)
  240. {
  241.     return common_transform(i_ctx_p, gs_transform, gs_point_transform);
  242. }
  243.  
  244. /* <dx> <dy> dtransform <dxt> <dyt> */
  245. /* <dx> <dy> <matrix> dtransform <dxt> <dyt> */
  246. private int
  247. zdtransform(i_ctx_t *i_ctx_p)
  248. {
  249.     return common_transform(i_ctx_p, gs_dtransform, gs_distance_transform);
  250. }
  251.  
  252. /* <xt> <yt> itransform <x> <y> */
  253. /* <xt> <yt> <matrix> itransform <x> <y> */
  254. private int
  255. zitransform(i_ctx_t *i_ctx_p)
  256. {
  257.     return common_transform(i_ctx_p, gs_itransform, gs_point_transform_inverse);
  258. }
  259.  
  260. /* <dxt> <dyt> idtransform <dx> <dy> */
  261. /* <dxt> <dyt> <matrix> idtransform <dx> <dy> */
  262. private int
  263. zidtransform(i_ctx_t *i_ctx_p)
  264. {
  265.     return common_transform(i_ctx_p, gs_idtransform, gs_distance_transform_inverse);
  266. }
  267.  
  268. /* Common logic for [i][d]transform */
  269. private int
  270. common_transform(i_ctx_t *i_ctx_p,
  271.     int (*ptproc)(P4(gs_state *, floatp, floatp, gs_point *)),
  272.     int (*matproc)(P4(floatp, floatp, const gs_matrix *, gs_point *)))
  273. {
  274.     os_ptr op = osp;
  275.     double opxy[2];
  276.     gs_point pt;
  277.     int code;
  278.  
  279.     /* Optimize for the non-matrix case */
  280.     switch (r_type(op)) {
  281.     case t_real:
  282.         opxy[1] = op->value.realval;
  283.         break;
  284.     case t_integer:
  285.         opxy[1] = op->value.intval;
  286.         break;
  287.     case t_array:        /* might be a matrix */
  288.     case t_shortarray:
  289.     case t_mixedarray: {
  290.         gs_matrix mat;
  291.         gs_matrix *pmat = &mat;
  292.  
  293.         if ((code = read_matrix(op, pmat)) < 0 ||
  294.         (code = num_params(op - 1, 2, opxy)) < 0 ||
  295.         (code = (*matproc) (opxy[0], opxy[1], pmat, &pt)) < 0
  296.         ) {        /* Might be a stack underflow. */
  297.         check_op(3);
  298.         return code;
  299.         }
  300.         op--;
  301.         pop(1);
  302.         goto out;
  303.     }
  304.     default:
  305.         return_op_typecheck(op);
  306.     }
  307.     switch (r_type(op - 1)) {
  308.     case t_real:
  309.         opxy[0] = (op - 1)->value.realval;
  310.         break;
  311.     case t_integer:
  312.         opxy[0] = (op - 1)->value.intval;
  313.         break;
  314.     default:
  315.         return_op_typecheck(op - 1);
  316.     }
  317.     if ((code = (*ptproc) (igs, opxy[0], opxy[1], &pt)) < 0)
  318.     return code;
  319. out:
  320.     make_real(op - 1, pt.x);
  321.     make_real(op, pt.y);
  322.     return 0;
  323. }
  324.  
  325. /* <matrix> <inv_matrix> invertmatrix <inv_matrix> */
  326. private int
  327. zinvertmatrix(i_ctx_t *i_ctx_p)
  328. {
  329.     os_ptr op = osp;
  330.     gs_matrix m;
  331.     int code;
  332.  
  333.     if ((code = read_matrix(op - 1, &m)) < 0 ||
  334.     (code = gs_matrix_invert(&m, &m)) < 0 ||
  335.     (code = write_matrix(op, &m)) < 0
  336.     )
  337.     return code;
  338.     op[-1] = *op;
  339.     pop(1);
  340.     return code;
  341. }
  342.  
  343. /* ------ Initialization procedure ------ */
  344.  
  345. const op_def zmatrix_op_defs[] =
  346. {
  347.     {"1concat", zconcat},
  348.     {"2dtransform", zdtransform},
  349.     {"3concatmatrix", zconcatmatrix},
  350.     {"0.currentmatrix", zcurrentmatrix},
  351.     {"1defaultmatrix", zdefaultmatrix},
  352.     {"2idtransform", zidtransform},
  353.     {"0initmatrix", zinitmatrix},
  354.     {"2invertmatrix", zinvertmatrix},
  355.     {"2itransform", zitransform},
  356.     {"1rotate", zrotate},
  357.     {"2scale", zscale},
  358.     {"6.setmatrix", zsetmatrix},
  359.     {"1.setdefaultmatrix", zsetdefaultmatrix},
  360.     {"2transform", ztransform},
  361.     {"2translate", ztranslate},
  362.     op_def_end(0)
  363. };
  364.